# -*- coding: utf-8 -*-
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from lxml import etree
import time
import re


class LingYing_Spider(object):
    def __init__(self):
        # GUI实例化，创建根窗口
        self.window = tk.Tk()
        # 根窗口添加标题
        self.window.title('领英自动化')
        # 根窗口固定大小
        self.window.geometry('800x600')
        # 嵌套一个提示窗口
        self.text_hint_1 = tk.Label(self.window, text='请输入查询的公司', bg='green', fg='white', font=12)
        self.text_hint_1.pack()
        # 嵌套一个输入窗口
        self.text_input = tk.Entry(self.window, font=12, show=None)
        self.text_input.pack()

        # 嵌套一个运行函数的按钮
        self.button = tk.Button(self.window, text='点击运行', font=12, bg='red', fg='white', command=self.login_html)
        self.button.pack()
        # 运行提示
        # self.text_hint_2 = tk.Text(self.window, width=85, height=20, font=12)
        # self.text_hint_2.pack()
        # 根窗口循环
        self.window.mainloop()

    def login_html(self):
        # 创建一个变量接受输入窗口的值
        str_company = self.text_input.get()
        # 实例化自动化对象
        driver = webdriver.Chrome()
        driver.get('https://www.linkedin.com/authwall?trk=ripf&trkInfo=AQED7qDsuPIMsgAAAX-5l4-AxlGf3Dw5x9hSclATswrbbkcwivzM6S2zWl1_IPtflN3Kiig76mHqmspc1Yv4y56TvYxw37LLA3VzFKg3hGMRIUn4oYXeAxBzy-JRuLwt3C40DXk=&originalReferer=&sessionRedirect=https%3A%2F%2Fwww.linkedin.com%2Fcompany%2Fwhite-lodging%2Fposts%2F%3FfeedView%3Dall')
        time.sleep(5)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//*[@id="main-content"]/div/form/p/button').click()
        # 输入账号
        session_key = driver.find_element(By.XPATH, '//*[@id="session_key"]')
        session_key.send_keys('wphdsb@163.com')
        # 输入密码
        session_password = driver.find_element(By.XPATH, '//*[@id="session_password"]')
        session_password.send_keys('19940820WPH')
        # 点击登录
        driver.find_element(By.XPATH, '//*[@id="main-content"]/div/div/div/form/button').click()
        time.sleep(3)
        # 点击搜索
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').click()
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').send_keys('White Lodging')
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').send_keys(Keys.ENTER)
        time.sleep(4)
        # 点击进入公司主页
        driver.find_element(By.XPATH, '//*[@id="main"]/div/div/div[1]/div/a/div/div[2]/div[2]/a/div/span').click()
        time.sleep(6)
        # 点击posts
        driver.find_element(By.XPATH, '//ul[@class="org-page-navigation__items "]/li[3]').click()
        # 进入循环
        for i in range(50):
            js = 'window.scrollTo(0,document.body.scrollHeight)'
            driver.execute_script(js)
            time.sleep(2)
            driver_html = driver.page_source

    def parse_html(self,driver_html):
        he = etree.HTML(driver_html)
        # 取出整个div
        div_html = he.xpath('//*[@id="main"]/div[2]/div/div[2]/div[3]/div/div[1]/div')
        print(div_html)
        # # 内容  (建议写正则)
        # neirong = he.xpath('//span[@class="break-words"]/span/span[@dir="ltr"]/text()')
        # # 点赞数
        # dianzan = he.xpath(''
        #                    '')
        # # 评论
        # pinlun = he.xpath('//button[@aria-label="1 comment on White Lodging’s post"]/span[@aria-hidden="true"]')
        # # 转发
        # zhuan = he.xpath('')



if __name__ == '__main__':
    a = LingYing_Spider()
